package com.gsbelarus.gedemin.income.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gsbelarus.gedemin.income.R;
import com.gsbelarus.gedemin.income.activity.Main;
import com.gsbelarus.gedemin.income.adapter.BalanceFrgmAdapter;
import com.gsbelarus.gedemin.income.interfaces.OnChangeBalanceFragment;
import com.gsbelarus.gedemin.lib.ui.viewpager.VP;

public class BalanceFrgmVP extends Fragment {

    public static final int INCOME_FRGM = 2;
    public static final int BALANCE_FRGM = 1;
    public static final int CLIENTS_FRGM = 0;

    private VP mPager;
    private BalanceFrgmAdapter mPagerAdapter;

    private int currentPage;

    private OnChangeBalanceFragment listner;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.view_pager, container, false);

        mPager = (VP) rootView.findViewById(R.id.pager);

        mPagerAdapter = new BalanceFrgmAdapter(getChildFragmentManager());
        mPager.setAdapter(mPagerAdapter);

        mPager.setCurrentItem(BALANCE_FRGM);

        mPager.setOffscreenPageLimit(2);

        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
                switch (position) {
                    case CLIENTS_FRGM:
                        mPager.setSliding(false);
                        listner.onChange(Main.DrawerMenuItem.CLIENTS_MONTH);
                        break;
                    case BALANCE_FRGM:
                        mPager.setSliding(true);
                        listner.onChange(Main.DrawerMenuItem.BALANCE);
                    case INCOME_FRGM:
                        mPager.setSliding(false);
                        listner.onChange(Main.DrawerMenuItem.INCOME);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        return rootView;
    }

    public void setOnChangeBalanceFragment(OnChangeBalanceFragment listner) {
        this.listner = listner;
    }

    public void showTrainingAnimation() {
        new Thread(new Runnable() {
            @Override
            public void run() {

                animateTo(-100);
                animateFrom(-100);
                animateTo(100);
                animateFrom(100);
            }
        }).start();
    }

    private void animateTo(final int toX) {
        final int sign = (toX < 0) ? -1 : 1;
        for (int i = 0; i <= Math.abs(toX); i += 2) {
            final int x = i;
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mPager.post(new Runnable() {
                @Override
                public void run() {
                    mPager.setScrollX(sign * x);
                }
            });
        }
    }

    private void animateFrom(final int fromX) {
        final int sign = (fromX < 0) ? -1 : 1;
        for (int i = Math.abs(fromX); i >= 0; i -= 2) {
            final int x = i;
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mPager.post(new Runnable() {
                @Override
                public void run() {
                    mPager.setScrollX(sign * x);
                }
            });
        }
    }

    public ViewPagerDateFrgm getCurrentFragment() {
        mPagerAdapter.startUpdate(mPager);
        ViewPagerDateFrgm frgm = (ViewPagerDateFrgm) mPagerAdapter.instantiateItem(mPager, currentPage);
        mPagerAdapter.finishUpdate(mPager);
        return frgm;
    }

    public ViewPager getViewPager() {
        return mPager;
    }
}
